﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoRentalShop.Controller;
using VideoRentalShop.View.Templates;
using VideoRentalShop.Model;
using System.Windows;
using System.Windows.Data;

namespace VideoRentalShop.View.Windows.ClientList
{
    public class ClientListPanel : ListPanel
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;
        private SearchPanel _panSearch;
        private EditPanel _panEdit;


        public ClientListPanel()
        {
            _panSearch = new SearchPanel();
            _panEdit = new EditPanel();
            AddSearchPanel(_panSearch);
            AddEditPanel(_panEdit);
            CreateListColumns();
        }


        protected override void Refresh()
        {
            _panSearch.Clear();
        }

        private void CreateListColumns()
        {
            AddListColumn("Imię", "Name");
            AddListColumn("Nazwisko", "LastName");
            AddListColumn("Pesel", "Pesel");
            AddListColumn("Email", "Email");
            var binding = new MultiBinding { StringFormat = "{0}, {1} {2}" };
            binding.Bindings.Add(new Binding { Path = new PropertyPath("Street") });
            binding.Bindings.Add(new Binding { Path = new PropertyPath("PostalCode") });
            binding.Bindings.Add(new Binding { Path = new PropertyPath("City") });
            AddListColumn("Adres", binding);
            AddListColumn("Zablokowany", "Blocked");
        }

        protected override void ClearClicked()
        {
            _panSearch.Clear();
        }

        protected override void SearchClicked()
        {
            var results = _panSearch.GetResults();
            ShowResults(results);
        }

        private void ShowResults(IEnumerable<Client> results)
        {
            var list = new List<dynamic>();

            foreach (var result in results)
            {
                list.Add(new
                {
                    Item = result,
                    Name = result.Name,
                    LastName = result.LastName,
                    Pesel = result.Pesel,
                    Email = result.Email,
                    Street = result.Street,
                    PostalCode = result.PostalCode,
                    City = result.City,
                    Blocked = result.Blocked ? "Tak" : "Nie"
                });
            }

            ShowListResults(list);
        }


        protected override void EditClicked()
        {
            if (lstResults.SelectedIndex > -1)
            {
                var completed = _panEdit.Edit();

                if (completed)
                {
                    RefreshList();
                }
            }
            else
            {
                _controller.ShowMessage("Wymagane jest uprzednie wybranie klienta z listy.", "Błąd", MessageType.Error);
            }
        }

        protected override void AddClicked()
        {
            var completed = _panEdit.Add();

            if (completed)
            {
                RefreshList();
            }
        }

        protected override void RemoveClicked()
        {
            var count = lstResults.SelectedItems.Count;
            var completed = 0;

            foreach (dynamic lstItem in lstResults.SelectedItems)
            {
                var item = (Client)lstItem.Item;

                var notReturnedCount = item.Orders.Where(x => !x.Archived).Count();

                if (notReturnedCount > 0)
                {
                    var result = MessageBox.Show("Klient posiada wypożyczone filmy. Czy chcesz go usunąć mimo to?", "Ostrzeżenie", MessageBoxButton.YesNo, MessageBoxImage.Warning);

                    if (result != MessageBoxResult.Yes)
                    {
                        return; 
                    }
                }

                if (_controller.Data.Client.Remove(item))
                {
                    ++completed;
                }
            }

            if (completed != count)
            {
                _controller.ShowMessage("Podczas usuwania klientów wystąpił błąd", "Błąd");
            }
            
            if (completed > 0)
            {
                RefreshList();
            }
        }

        private void RefreshList()
        {
            SearchClicked();
        }

        protected override void SelectionChanged()
        {
            var itemsCount = lstResults.SelectedItems.Count;

            if (itemsCount > 0)
            {
                dynamic lstItem = lstResults.SelectedItem;
                var item = lstItem.Item as Client;

                if (item != null)
                {
                    _panEdit.LoadItem(item);
                }
            }
        }

    }
}
